/*
 * File Name: AbstractBaseSearchPanel.java
 */

package co.edu.uniandes.intercon.client.gui;

import javax.swing.JTable;

import co.edu.uniandes.intercon.client.model.IBaseTableModel;

/**
 * Defines the base behaviour for the search panels
 * 
 * @author Fabian Gutierrez [fagossa@gmail.com]
 */
public abstract class AbstractBaseSearchPanel<E> extends AbstractBasePanel {
    
    private IBaseTableModel<E> localTableModel;
    private IBaseTableModel<E> remoteTableModel;
        
    /**
     * Retrieves the current record of the grid
     * 
     * @return E Current record selected in the grid
     */
    public E getCurrentLocalRecord() {
        int row = getCurrentLocalTable().getSelectedRow();
        E record = null;
        if (row >=0) {
            record = getLocalTableModel().getRecord(row);
        }
        return record;
    }
    
    /**
     * Retrieves the current record of the grid
     * 
     * @return E Current record selected in the grid
     */
    public E getCurrentRemoteRecord() {
        int row = getCurrentRemoteTable().getSelectedRow();
        E record = null;
        if (row >=0) {
            record = getRemoteTableModel().getRecord(row);
        }
        return record;
    }
    
    /**
     * Update the record specified in the grid
     * 
     * @param record Record to be updated in the current position of the grid
     */
    public void updateCurrentLocalRecord(E record) {
        int row = getCurrentLocalTable().getSelectedRow();
        if (row >=0) {
            getLocalTableModel().setRecord(row, record);
        }
    }
    
    /**
    * Update the record specified in the grid
    * 
    * @param record Record to be updated in the current position of the grid
    */
   public void updateCurrentRemoteRecord(E record) {
       int row = getCurrentRemoteTable().getSelectedRow();
       if (row >=0) {
           getRemoteTableModel().setRecord(row, record);
       }
   }
    
    /**
     * Retrieves the current table model instance
     * 
     * @return TableModel instance
     */
    public IBaseTableModel<E> getLocalTableModel() {
        return localTableModel;
    }
    
    /**
     * Retrieves the current table model instance
     * 
     * @return TableModel instance
     */
    public IBaseTableModel<E> getRemoteTableModel() {
        return remoteTableModel;
    }
    
    /**
     * Sets the new table model instance
     * 
     * @param tableModel Instance of the table model to be added
     */
    public void setLocalTableModel(IBaseTableModel<E> tableModel) {
        this.localTableModel = tableModel;
    }
    
    /**
     * Sets the new table model instance
     * 
     * @param tableModel Instance of the table model to be added
     */
    public void setRemoteTableModel(IBaseTableModel<E> tableModel) {
        this.remoteTableModel = tableModel;
    }
   
    /**
     * Edits the current record in the table
     */
    public abstract void editCurrentRecord();
    
    /**
     * Returns the instance of the current table
     * 
     * @return Table used to show records
     */
    public abstract JTable getCurrentLocalTable();
    
    /**
     * Returns the instance of the current table
     * 
     * @return Table used to show records
     */
    public abstract JTable getCurrentRemoteTable();
}
